package com.py.credit.service.impl;

import com.py.credit.common.ServiceException;
import com.py.credit.constant.CreditRepairType;
import com.py.credit.constant.RecordStatusEnum;
import com.py.credit.dao.CreditRepairApplyDao;
import com.py.credit.entity.CreditRepairApply;
import com.py.credit.service.CreditRepairService;
import com.py.credit.utils.NumberUtils;
import com.py.credit.utils.SQLBuilder;
import com.py.credit.utils.UuidUtil;
import com.py.framework.core.common.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * 信用修复服务类
 *
 * @author YaoS
 * @date 19/4/10 14:46
 */
@Service
public class CreditRepairServiceImpl implements CreditRepairService {

    @Autowired
    private CreditRepairApplyDao creditRepairDao;

    @Override
    public List<Map<String, Object>> getByConditionForPage(String tableId, List<String> showColumns,
                                                           String[] additionalColumns, String condition, Page page) {
        if (CollectionUtils.isEmpty(showColumns)) {
            throw new ServiceException("至少选择一列导出");
        }
        List<String> conditions = SQLBuilder.createConditionSql(condition);
        if (CreditRepairType.FR_XZCF.toString().equals(tableId) || CreditRepairType.ZRR_XZCF.toString().equals(tableId)) {
            return creditRepairDao.selectByConditionForPage(tableId, showColumns, additionalColumns, conditions, page);
        } else {
            return creditRepairDao.selectHeiByConditionForPage(tableId, showColumns, additionalColumns, conditions, page);
        }
    }


    @Override
    public void addCreditRepairHistory(Integer tableId, String recordId, String repairSubject, String deptName, String user,
                                       String cfSy, String cfWsh, String cfYj) {
        CreditRepairApply creditRepairApply = new CreditRepairApply();
        creditRepairApply.setkId(tableId);
        creditRepairApply.setRepairDept(deptName);
        creditRepairApply.setRepairTime(new Date());
        creditRepairApply.setRecordId(Integer.valueOf(recordId));
        creditRepairApply.setRepairUser(user);
        creditRepairApply.setCfSubject(repairSubject);
        creditRepairApply.setCfSy(cfSy);
        creditRepairApply.setCfWsh(cfWsh);
        creditRepairApply.setCfYj(cfYj);
        if (CreditRepairType.FR_XZCF.equals(tableId) || CreditRepairType.ZRR_XZCF.equals(tableId)) {
            creditRepairDao.insertSelective(creditRepairApply);
        } else {
        	if(cfYj != null && !"".equals(cfYj)){
        		Long timeLong = Long.parseLong(cfYj);
		        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
		        Date date;
				try {
					date = format.parse(format.format(timeLong));
					cfYj =  format.format(date);
				} catch (ParseException e) {
					e.printStackTrace();
					cfYj = null;
				}
        	}
        	creditRepairApply.setCfYj(cfYj);
        	
            creditRepairDao.insertSelectiveByHei(creditRepairApply);
        }

    }

    @Override
    public void repair(String recordId, String tableName) {
        creditRepairDao.updateCfStatusById(recordId, RecordStatusEnum.FIX.getValue(), tableName);
    }

    @Override
    public Map<String, Object> getRecordById(String recordId, String tableName) {
        return creditRepairDao.selectCfDetail(recordId, tableName);
    }

    @Override
    public List<Map<String, Object>> getCfRecordsByConditionForPage(String tableName, List<String> showColumns,
                                                                    String[] additionalColumns, String condition, Page page, String deptId) {
        if (CollectionUtils.isEmpty(showColumns)) {
            throw new ServiceException("至少选择一列");
        }
        List<String> conditions = SQLBuilder.createConditionSql(condition);
        List<Map<String, Object>> selectRecordsByConditionForPage = creditRepairDao
                .selectCfRecordsByConditionForPage(tableName, showColumns, additionalColumns, conditions, page, deptId);

        // 身份证脱敏
        if (showColumns.contains("B000002")) {
            if (!CollectionUtils.isEmpty(selectRecordsByConditionForPage)) {
                for (Map<String, Object> map : selectRecordsByConditionForPage) {
                    String idEncrypt = NumberUtils
                            .idEncrypt(map.get("B000002") != null ? map.get("B000002").toString() : "");
                    map.put("B000002", idEncrypt);
                }
            }
        }
        return selectRecordsByConditionForPage;
    }


	
	/* (非 Javadoc)
	 *
	 
	 *
	 
	 * @return
	 * @see com.py.credit.service.CreditRepairService#updateRepairHistoryTask()
	 */
	@Transactional
	@Override
	public Object updateRepairHistoryTask() {
		//与前置机信用修复数据进行比对
		List<Map<String,Object>> data = creditRepairDao.getRepairData();
		for(Map<String,Object> map :data) {
			creditRepairDao.updateRepairDataStatus(String.valueOf(map.get("record_id")),String.valueOf(map.get("BZ")));
		}
		//清除所有修复数据
		creditRepairDao.clearRepairHistory();
		//新增修复数据
		creditRepairDao.addRepairHistory();
		
		
		return null;
	}

}
